<template>
  <div class="edu-page-warp">
    <el-card shadow="never" body-style="padding: 24px;" class="card-margin">
        <h1 class="condition-title">审批列表</h1>
        <el-form ref="queryForm" 
            :model="queryForm" 
            label-position="right" 
            label-width="90px" 
            @submit.native.prevent>
                <el-row :gutter="24" style="height: 50px;">
                    <el-col :span="6" v-if="showDepartment">
                        <el-form-item :label="$t('culture.cultureScheme.proDept')+':'">
                            <el-select
                                v-model="query.projId"
                                style="width: 100%"
                                @change="searchApprove"
                            >
                                <el-option
                                    v-for="item in options.project"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="申请人:">
                            <el-input v-model="query.applicantName" placeholder="申请人" style="width:240px" @keyup.enter.native="searchApprove">
                                <i class="el-icon-search" slot="suffix" style="margin-top:10px;cursor:pointer" @click="searchApprove">
                                </i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        <div class="clearfix">
            <div class="float-right">
                <el-button type="primary" @click="batchPlan"
                >批量审批</el-button>
            </div>
        </div>
        <el-table stripe :data="data" size="mini" style="margin-top:10px" @selection-change="chooseStudent" tooltip-effect="light">
            <el-table-column type="selection" width="50"></el-table-column>               
            <el-table-column prop="no" label="申请编号" ></el-table-column>
            <el-table-column prop="applicantName" label="申请人" ></el-table-column>
            <el-table-column prop="applicantId" label="申请人编号" ></el-table-column>
            <el-table-column label="申请时间" >
                <template slot-scope="scope">{{ scope.row.time | formatDate }}</template>
            </el-table-column>
            <el-table-column  label="审批状态" >
                <template slot-scope="scope">
                    <el-tag
                    v-if="scope.row.result == 'Process'"
                    type="success">进行中</el-tag>
                    <el-tag
                    v-if="scope.row.result == 'End'"
                    type="info">已结束</el-tag>
                    <el-tag
                    v-if="scope.row.result == 'Reject'"
                    type="warning">已驳回</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="approvorName" label="下一级审批人" min-width="200px" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" width="90">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="showRollDetail(scope.row.no,scope.row.projId)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="edu-pagination">
        <div class="float-right">
          <el-pagination
            :current-page="query.pageNum"
            :page-sizes="[20,50,100,500,1000]"
            :page-size="query.pageSize"
            :total="query.total_"
            layout="total, sizes, prev, pager, next, jumper"
            style="float: right;margin:10px 60px 0 0;"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"/>
        </div>
      </div>
     </el-card>
  </div>
</template>
<script>
import { selectTaskList,selectTaskListPage } from 'common/src/api/approval';
import { schemePlanAddSet, schemePlanfindResult, batchPlan } from "common/src/api/culture/culturePlan";
import { queryDic} from "common/src/api/dictionary";
import {judgeManagement} from '../culturescheme/common';
import filters from 'common/src/filters'
import mixin from 'common/src/mixins/index';
export default {
  name: 'culturePlanApproveList',
  mixins: [mixin],
  filters: {
    formatDate(date) {
      return filters.formatDate(date, 'yyyy/MM/dd hh:mm:ss')
    }
  },
  mounted(){
        // let currentUser =
        //     (this.$store.state.session && this.$store.state.session.user) ||
        //     JSON.parse(localStorage.getItem("user"));
        // if (currentUser.currentRole == 1) {
        //     this.projId = currentUser.currentManageDptId;
        // }
        // let projId = judgeManagement(this.$route.path)
        let currentUser =this.global.currentUser;
            if(currentUser.currentRole == 1 ) {
                // this.queryForm.projId = currentUser.currentManageDptId;
                console.log(this.global.projectId)
                this.query.projId = this.global.projectId;
                this.getApproveList()
            } else {
                this.initProjs();
            }
            this.currentRole = currentUser.currentRole;

        this.getTime();
        this.getTime2();
      
  },
  data(){
      return {
        query:{
            condition:'',
            wfType:18,
            show:"1",
            pageNum:1,
            pageSize:20,
            total_:0,
            projId:''
        },
        searchWord:'',
        startTime:'',
        endTime:'',
        startTime1:'',
        endTime1:'',
        data:[],
        choosePlan:[],
        options: {
            project: [],
        },
        showDepartment: false,
      }
  },
  methods: {
    async initProjs(){
        let params = {
            lang: this.$store.getters.language || "cn",
            type: "allChild",
            keys: ["X_GLBM"]
        };
        let res = await queryDic(params);
        let user = this.global.currentUser;
        // let {manageDptIds} = user;
        //     manageDptIds=manageDptIds. judgeManagement(this.$router)
        // let {code, data: {X_GLBM}} = res;
        // if (code === 200) {
        //     let dpts = Object.keys(X_GLBM).map(item => ({
        //         label: X_GLBM[item],
        //         value: item + ""
        //     }))
        //     let list=judgeManagement.call(this,this.$route.path);
        //     this.options.project = dpts.filter(item => {
        //         return list.indexOf(item.value) > -1;
        //     }) 
        // }
        let {code, data: {X_GLBM}} = res;
        if (code === 200) {
            let dpts = Object.keys(X_GLBM).map(item => ({
                label: X_GLBM[item],
                value: item + ""
            }))
            this.options.project = dpts.filter(item => {
                return item.value != 1;
            }) 
        }
        console.log(this.options.project)
        //this.queryForm.projId = this.options.project[0].value;
        this.query.projId = this.options.project[0].value;
        this.initCurrentRole();
        this.getApproveList();
    },
    initCurrentRole() {
        let currentUser =this.global.currentUser;
        this.showDepartment = currentUser.currentRole == "2"&&this.options.project.length>1;
    },
    getTime(){
        schemePlanfindResult({
            projIdStr:"2",
            projId:"",
        }).then(res =>{
            if(res.code == 200){
                this.startTime = res.data.startTime
                this.endTime = res.data.stopTime       
            }
        })
    },
    getTime2(){
        let data2 = { 
            projIdStr:"4",
            projId:"",
        }
        schemePlanfindResult(data2).then(res =>{
            if(res.code == 200){
                this.startTime1 = res.data.startTime
                this.endTime1 = res.data.stopTime       
            }
        })
    },
    getApproveList(isFirstPage=true){
    if(isFirstPage){
        this.query.pageNum=1;
    }
    selectTaskList(this.query).then(res=>{
        if(res.code==200){
            this.query.total_ = res.data.total_;
            this.data = res.data.list;
        }
    })
    },
    //选择复选框
    chooseStudent(val) {
        this.choosePlan = val;
        console.log(this.choosePlan)
    },
    batchPlan(){
        let data = [];
        if(this.choosePlan.length == 0){
            this.$message({
                    type: "warning",
                    message: "请先选择要审批的培养计划"
                });
            return;
        }else{
            console.log(this.query.projId)
            let now = new Date().getTime();
            if(this.query.projId == 2){
                if(now > this.endTime || now < this.startTime){
                this.$message({
                    type: "warning",
                    message: "不在审批范围时间内"
                });
                return;
                }
            }
            if(this.query.projId == 4){
                if(now > this.endTime1 || now < this.startTime1){
                this.$message({
                    type: "warning",
                    message: "不在审批范围时间内"
                });
                return;
                }
            }
            let that = this
            this.choosePlan.forEach(d =>{
                if(d.projId == 2){
                    data.push({
                        "no":d.no,
                        "startTime":that.startTime,
                        "endTime":that.endTime
                    })
                }else if(d.projId == 4){
                    data.push({
                        "no":d.no,
                        "startTime":that.startTime1,
                        "endTime":that.endTime1
                    })
                }
                
            })
            batchPlan(data).then(res=>{
                if(res.code==200){
                    this.$message({
                        type: "success",
                        message: "审批成功"
                    });
                    this.getApproveList();
                }
            })
        }

        console.log(this.choosePlan)
    },
    searchApprove(){
        this.getApproveList(true);
    },
    handleSizeChange(val){
        this.query.pageSize=val;
        this.getApproveList();
    },
    handleCurrentChange(val){
        this.query.pageNum=val;
        this.getApproveList(false);
    },
    showRollDetail(id,projId){
        this.$router.push('/approveDetail?id=' + id + '&projId=' + projId)
        // this.$router.push('/approveDetail?id=' + id)
    }
  }
}
</script>
<style lang="scss" scoped>
 .div-main {
        margin-bottom: 16px;
    }
</style>
